/*
 * $Id: SearchPanel.java,v 1.3 2005/04/23 19:52:18 keilw Exp $
 * 
 * SearchPanel.java
 *
 * Created on 18. Dezember 2001, 00:13
 *
 * @author  Werner Keil
 */

package org.medbeans.modules.patients;

import java.awt.Component;
import java.awt.Dialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
/*import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;*/

import javax.swing.JButton;
import javax.swing.JComboBox;

import org.openide.TopManager;
import org.openide.DialogDescriptor;
import org.openide.util.HelpCtx;
import org.openide.util.NbBundle;

/**
 * @version $Revision: 1.3 $ $Date: 2005/04/23 19:52:18 $
 * @author  Werner Keil
 */
public class SearchPanel extends javax.swing.JPanel {
    /**
	 * 
	 */
	private static final long serialVersionUID = 3257849874568198455L;

	/** Return status code - returned if Cancel button has been pressed. */
    public static final int RET_CANCEL = 0;
    
    /** Return status code - returned if OK button has been pressed. */
    public static final int RET_OK = 1;
    
    /** Dialog descriptor. */
    private DialogDescriptor dialogDescriptor;
    
    /** OK button. */
    private JButton okButton;
    
    /** Cancel button. */
    private JButton cancelButton;
    
    /** Java equivalent. */
    private Dialog dialog;
    
    /** Return status. */
    private int returnStatus = RET_CANCEL;
    
    /** Whether some criterion is customized. */
    private boolean customized;
    
    /** Creates new form SearchPanel */
    public SearchPanel() {
        initComponents();
        tabbedPane.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                tabbedPaneStateChanged(evt);
            }
        });
        
        setName(NbBundle.getBundle(SearchPanel.class).getString("TEXT_TITLE_CUSTOMIZE")); // NOI18N
        
        okButton = new JButton(NbBundle.getBundle(SearchPanel.class).getString("TEXT_BUTTON_SEARCH")); // NOI18N
        okButton.setEnabled(isCustomized());
        
        cancelButton = new JButton(NbBundle.getBundle(SearchPanel.class).getString("TEXT_BUTTON_CANCEL")); // NOI18N
        
        Object options[] = new Object[] {okButton, cancelButton};
        /** @todo Add ChangeListener for combo/phonCheck to ENABLE/disable OK button
         * @todo Add KeyListener for combo to react to ESC/RETURN as well (does not work in combo...)
         */
        initAccessibility();
        
        // Creates representing dialog descriptor.
        dialogDescriptor = new DialogDescriptor(
        this,
        getName(),
        true,
        options,
        options[0],
        DialogDescriptor.BOTTOM_ALIGN,
        getHelpCtx(),
        new ActionListener() {
            public void actionPerformed(final ActionEvent evt) {
                if(evt.getSource() == okButton) {
                    doClose(RET_OK);
                } else {
                    doClose(RET_CANCEL);
                }
            }
        });
    }
    
    private void initAccessibility(){
        this.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(SearchPanel.class).getString("ACS_SearchPanel")); // NOI18N
        nameLabel.getAccessibleContext().setAccessibleName(NbBundle.getBundle(SearchPanel.class).getString("LBL_Pat_Name"));
        //nameLabel.getAccessibleContext().setAccessibleName(NbBundle.getBundle(SearchPanel.class).getString("LBL_Pat_Name"));
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        java.awt.GridBagConstraints gridBagConstraints;

        tabbedPane = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        nameLabel = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        nameCombo = new javax.swing.JComboBox();
        phonCheck = new javax.swing.JCheckBox();

        setLayout(new java.awt.BorderLayout());

        jPanel1.setLayout(new java.awt.GridBagLayout());

        nameLabel.setText("<Label1>");
        nameLabel.setLabelFor(nameCombo);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 0, 0);
        jPanel1.add(nameLabel, gridBagConstraints);

        jLabel2.setText(" ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
        gridBagConstraints.weightx = 0.1;
        gridBagConstraints.weighty = 0.1;
        jPanel1.add(jLabel2, gridBagConstraints);

        nameCombo.setEditable(true);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(10, 5, 0, 0);
        jPanel1.add(nameCombo, gridBagConstraints);

        phonCheck.setText("<Phonetic>");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 0);
        jPanel1.add(phonCheck, gridBagConstraints);

        tabbedPane.addTab("<Name>", jPanel1);

        add(tabbedPane, java.awt.BorderLayout.NORTH);

    }//GEN-END:initComponents
    
    public void requestFocus() { //set focus for one components (myField in this case)
        nameCombo.requestFocus();
    }
    
    /** Shows dialog created from <code>DialogDescriptor</code> which wraps this instance. */
    public void showDialog()  {
        dialog = TopManager.getDefault().createDialog(dialogDescriptor);
        dialog.setModal(true);
        dialog.addComponentListener(new ComponentAdapter() {
            public void componentShown(ComponentEvent evt) {
                int selectedIndex = tabbedPane.getSelectedIndex();
                if(selectedIndex < 0) selectedIndex = 0;
                
                //Component component = getTypeCustomizer(selectedIndex);
                Component component = (Component)getNameField();
                if (component != null)
                    component.requestFocus();
                dialog.removeComponentListener(this);
            }
        });
        
        dialog.pack();
        dialog.setVisible(true);
    }
    
    /** Closes dialog. */
    private void doClose(int returnStatus) {
        this.returnStatus = returnStatus;
        
        if (dialog!=null) {
            dialog.setVisible(false);
            dialog.dispose();
        }
    }
    
    /** Gets help context. */
    private HelpCtx getHelpCtx() {
        int index = tabbedPane.getModel().getSelectedIndex();
        /*SearchTypePanel panel = (SearchTypePanel)getOrderedSearchTypePanels().get(index);
         
        return panel.getHelpCtx();
         */
        /** @todo add real help (based on tab?) */
        return HelpCtx.DEFAULT_HELP;
    }
    
    private void tabbedPaneStateChanged(javax.swing.event.ChangeEvent evt) {
        /* Only exec. code disabled, because of issue #17737
        Component component = getTypeCustomizer(tabbedPane.getSelectedIndex());
        if(component != null)
            component.requestFocus();
         */
    }
    
    /** Getter for property nameField.
     * @return Value of property nameField.
     */
    public JComboBox getNameField() {
        return nameCombo;
    }
    
    /** Getter for property returnStatus.
     * @return Value of property returnStatus.
     */
    public int getReturnStatus() {
        return returnStatus;
    }
    
    /** Getter for property customized.
     * @return Value of property customized.
     */
    public boolean isCustomized() {
        return customized;
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel nameLabel;
    private javax.swing.JTabbedPane tabbedPane;
    private javax.swing.JCheckBox phonCheck;
    private javax.swing.JComboBox nameCombo;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables
    
}
